import { ref, reactive } from "vue";
import type { FormRules } from "element-plus";
import { checkCaptchaCode } from "@/api/auth";

/** 登录校验 */
const loginRules = reactive(<FormRules>{
  code: [
    {
      validator: async (_, value, callback) => {
        if (value === "") {
          callback(new Error("请输入验证码"));
        } else {
          await checkCaptchaCode({
            code: value,
            timestamp: captchaTimestamp.value
          })
            .then(res => {
              if (!res) {
                callback(new Error("验证码错误"));
              } else {
                callback();
              }
            })
            .catch(() => {
              callback(new Error("验证码错误"));
            });
        }
      },
      trigger: "blur"
    }
  ]
});
const captchaTimestamp = ref(Date.now().valueOf());

export { loginRules, captchaTimestamp };
